//
//  Third.m
//  A段项目
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "Third.h"
#import "NetWorkingHandler.h"
#import "Model.h"
#import <MBProgressHUD.h>
#import "ThirdStyleCell.h"
#import  <MJRefresh.h>
#import "ActivityOfThird.h"
#import <DKNightVersion.h>

@interface Third ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation Third

- (void)dealloc {
    
    self.tableView.delegate = nil;
    [_arrModel release];
    [_tableView release];
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    
  
    
    [super viewDidLoad];

    [self dataArray];
    
    [self handleData]; //获取数据
    
    [self createTableView];
    
    [self addHud]; // 添加 HUD
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self.arrModel removeAllObjects];
        
        [self handleData];
    
    }];

}

- (void)addHud {
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
  
    
}

- (NSArray *)dataArray {
    
    if (self.arrModel == nil) {
        
        
        
        self.arrModel = [NSMutableArray array];
    }
    
    return self.arrModel;
}

- (void)handleData {
    
    [NetWorkingHandler netWorkingWithUrl:@"http://mapp.tiankong.com/activity/list" complete:^(id result) {
       
        NSArray *array = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            
            Model *model = [[Model alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrModel addObject:model];
            
            [model release];
            
          
            
        }

        [self.tableView reloadData];
        
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.separatorStyle = 0;
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [self.tableView registerClass:[ThirdStyleCell class] forCellReuseIdentifier:@"pool1"];
    
    [_tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.arrModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ThirdStyleCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    cell.selectionStyle = 0;
    
    Model *model = [self.arrModel objectAtIndex:indexPath.row];
    
    [cell passedInfo:model];
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return 270;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Model *model = [self.arrModel objectAtIndex:indexPath.row];
    
    ActivityOfThird *actity = [[ActivityOfThird alloc] init];
    
    actity.model = model;
    
    [self.navigationController pushViewController:actity animated:YES];
    
    [actity release];
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);

    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
